namespace FIREFLYX {
    /**画一个cube */
    export class DrawCube implements IExample {
        public Init(canvas: HTMLCanvasElement): void {
            let gl = canvas.getContext("webgl2");
            //app init
            App.init(gl.canvas as any);
            let vs = `#version 300 es
            layout (location = 0) in vec4 a_Position; //vec 是有默认值的 (0,0,0,1)
            out vec3 v_pos;
            void main() {
                float ang_x = 45.0/ 57.2957;
                mat4 u_rotMat_x = mat4(1.0, 0.0,        0.0,         0.0,
                                       0.0, cos(ang_x), -sin(ang_x), 0.0,
                                       0.0, sin(ang_x),  cos(ang_x), 0.0,
                                       0.0, 0.0,         0.0,        1.0 );
                float ang_y = 45.0 / 57.2957;
                mat4 u_rotMat_y = mat4( cos(ang_y), 0.0, sin(ang_y), 0.0,
                                        0.0,        1.0, 0.0,        0.0,
                                        -sin(ang_y),0.0, cos(ang_y), 0.0,
                                        0.0,        0.0, 0.0,        1.0 );
                mat4 rotMat = u_rotMat_x * u_rotMat_y;
                // mat4 rotMat = u_rotMat_y;
                v_pos = a_Position.xyz;
                gl_Position = rotMat * a_Position;
            }
            `;

            let fs = `#version 300 es
                precision mediump float;
                in vec3 v_pos;
                out vec4 color;
                void main(){
                    color = vec4(v_pos.xyz,1.0) * 2.0;
                    //color = vec4(1.0,0.0,0.0,1.0);
                }
            `;

            //init Shaders
            let sp = InitShader(gl, vs, fs);
            //使用 gl 程序
            gl.useProgram(sp);

            // let md = GenCubeData(0.5);
            let mesh = DefaultAsset.GetMesh(DefMesh.CUBE);
            //vao
            gl.bindVertexArray(mesh.VAO);   //启用vao

            //绘制
            setTimeout(() => {
                gl.drawElements(gl.TRIANGLES, mesh.elementLenght, mesh.elementType, 0);
            }, 0);
        }

    }
}